from datetime import date

from django.http import HttpResponse
from django.shortcuts import render_to_response

from parse_today import events_of_the_day, transform_event_infos

def first_function(request):
    return HttpResponse("Hello, world. Welcome!")

def get_todays_events(request):
    return get_events(request, date.today().strftime('%B_%d'))

def get_events(request, todays_date):

    event_infos = events_of_the_day(todays_date)

    if event_infos is None:
        return HttpResponse("Looks like something bad happened. Unable to fetch data. Please try later.")

    if event_infos == 404:
        return HttpResponse("The page you requested is not found.")
    
    event_info_list = transform_event_infos(event_infos)

    if event_info_list is None:
        return HttpResponse("Looks like something bad happened. Unable to fetch data. Please try later.")
    else:
	day = todays_date.replace('_',' ')
	return render_to_response('events.html', {'event_info_list':event_info_list, 
		                                  'day':day, 
						  'wiki_day':todays_date})
